package mks.plugin;

import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Shell;

public class MKSCommand {

    private Shell shell;

    public MKSCommand(Shell shell) {
        super();
        this.shell = shell;
    }

    private void runMKS(String command, String path, File dir) {
        runMKS(generateMKSGUICommandLine(path, dir, command), dir);
    }

    private void runMKS(List<String> arguments, final File dir) {
        arguments.add(0, "si");

        final List<String> monitorArgs = arguments;

        ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);

        try {
            dialog.run(true, true, new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor) {
                    monitor.beginTask("Executing MKS command ...", 100);
                    try {
                        // Process p = Runtime.getRuntime().exec(args, null, dir);
                        ProcessBuilder processBuilder = new ProcessBuilder(monitorArgs);
                        processBuilder.directory(dir);
                        processBuilder.redirectErrorStream(true);
                        Process process = processBuilder.start();
                        OutputStream stream = process.getOutputStream();

                        process.waitFor();

                        if (process.exitValue() != 0) {
                            // Checking for error codes. Nonzero indicates the command was executed,
                            // but there were errors.
                            MessageDialog.openError(shell, "MKS Error", "Error executing MKS command: exit code:" + process.exitValue());
                        }
                    }
                    catch (Exception e) {
                        MessageDialog.openError(shell, "MKS Error", e.getMessage());
                    }
                    finally {
                        monitor.done();
                    }
                }
            });
        }
        catch (Exception e) {
            MessageDialog.openError(shell, "MKS Error", e.getMessage());
        }

    }

    protected List<String> generateMKSGUICommandLine(String path, File dir, String command) {
        List<String> args = new ArrayList<String>();
        args.add(command);
        args.add("--gui");
        args.add(path);

        return args;
    }

    public void checkout(String path, File dir) {
        runMKS("co", path, dir);
    }

    public void checkin(String path, File dir) {
        runMKS("ci", path, dir);
    }

    public void revisionInfo(String path, File dir) {
        runMKS("revisioninfo", path, dir);
    }

    public void resync(String path, File dir) {
        runMKS("resync", path, dir);
    }

    public void viewHistory(String path, File dir) {
        runMKS("viewhistory", path, dir);
    }

    public void diff(String path, File dir) {
        runMKS("diff", path, dir);
    }

    public void lock(String path, File dir) {
        runMKS("lock", path, dir);
    }

    public void add(String path, File dir) {
        runMKS("add", path, dir);
    }
}
